//
//  QuickRegViewController.m
//  安好
//
//  Created by 冯凯 on 14-12-5.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "QuickRegViewController.h"
#import "RegistrationCell.h"

#import "RegionHospitalDeskViewController.h"
//地区modal
#import "AreaModal.h"
//医院modal
#import "RegistrationHospitalModal.h"

#import "RegDoctorListViewController.h"

@interface QuickRegViewController ()<RegionHospitalDeskViewDelegate>

/**
 *  点击选中行的cell
 */
@property(nonatomic,strong)RegistrationCell * selectedCell;
/**
 *  点击选中行的位置IndexPath可以获取到第几行
 */
@property(nonatomic,strong)NSIndexPath * selectedIndexPath;
/**
 *  得到返回来的“地区”省modal
 */
@property(nonatomic,strong)AreaModal * getBackProvinceModal;
/**
 *  得到返回来的“市”modal
 */
@property(nonatomic,strong)AreaModal * getbackCityModal;
/**
 * 得到返回来的“医院modal”
 */
@property(nonatomic,strong)RegistrationHospitalModal * getBackHospitalModal;

//快速挂号tableView
@property (strong, nonatomic) UITableView *tableView;
/**
 *  table右边cell 选择地区，选择医院，选择科室数组
 */
@property(strong,nonatomic)NSArray * selecteTypes;
/**
 *  快速挂号--》信息，省provinceid，市cityid，医院hospitalid，一级科室parhospdeptid，二级科室hospdeptid
 */
@property(strong,nonatomic)NSMutableDictionary * finishedDic;
//
@property(nonatomic,strong)NSMutableArray * getDataArray;
//确认挂号按钮
@property(nonatomic,strong)UIButton * registrationButton;


@end

@implementation QuickRegViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
    self.finishedDic = [NSMutableDictionary dictionaryWithCapacity:0];
    [self makeNavBar];
    [self loadData];
    [self makeTableView];
}
#pragma mark - 导航条设置
-(void)makeNavBar
{
    self.navigationItem.title = @"快速挂号";
    //左按钮--返回
    UIBarButtonItem *backBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backClicked)];
    [backBtnImg setTintColor:[UIColor whiteColor]];
    self.navigationItem.leftBarButtonItem = backBtnImg;
}
//返回
-(void)backClicked
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 加载数据
-(void)loadData
{
    self.getDataArray = [NSMutableArray arrayWithCapacity:0];
    [self.getDataArray addObject:@""];
    self.selecteTypes = @[@"选择地区",@"选择医院",@"选择科室"];
    
}
#pragma mark - 创建tableView
-(void)makeTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 10, kScreenWidth, kScreenHeight-iOS7TopMargin)];
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = [ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //隐藏横向和竖向的滑条
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

#pragma mark - tableView的代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.getDataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RegistrationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"RegistrationCell" owner:self options:nil] lastObject];
    }
    MyNSLog("%d", indexPath.row);
    cell.registrationLable.text = self.selecteTypes[indexPath.row];

    cell.detailLabel.text = self.getDataArray[indexPath.row];
    cell.detailLabel.frame = CGRectMake(132+15, 13, 143, 17);
    
    [cell.accessoryTypeImgView setImage:[UIImage imageNamed:@"公用_跳转.png"]];
    cell.topLineView.backgroundColor = [ClassMethodsViewController colorWithHexString:LINE_BACKGROUNDCOLOR_16];
    cell.bottomLineView.backgroundColor = [ClassMethodsViewController colorWithHexString:LINE_BACKGROUNDCOLOR_16];
    //只有首行cell有top线，全长
    if (indexPath.row != 0)
    {
        cell.topLineView.hidden = YES;
    }
    else
    {
        cell.topLineView.hidden = NO;
        cell.topLineView.frame = CGRectMake(0, 0, kScreenWidth, 0.5);
    }
    //只有末行cell的bottom线全长，其余行不是全长
    if (indexPath.row == self.getDataArray.count-1  )
    {
        cell.bottomLineView.frame = CGRectMake(0, 43, kScreenWidth, 0.5);
    }
    else
    {
        cell.bottomLineView.frame = CGRectMake(15, 43, kScreenWidth-20, 0.5);
    }
    
    return cell;
    
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.registrationButton removeFromSuperview];
    
    //获取到选中的是哪一行
    self.selectedIndexPath = indexPath;
    //获取到选中行的cell
    self.selectedCell = (RegistrationCell *)[self.tableView cellForRowAtIndexPath:indexPath];
    RegionHospitalDeskViewController * ReHosDesk = [[RegionHospitalDeskViewController alloc] init];
    ReHosDesk.delegate = self;
    if (indexPath.row == 0) {
        //选中的时“地区”--过去需要重新请求地区
        ReHosDesk.provinceid = @"0";
        ReHosDesk.loadRequestNum = @"0";
    }
    //选择“医院”
    if (indexPath.row == 1) {
        ReHosDesk.loadRequestNum = @"1";
        //把省modal和市modal传过去
        ReHosDesk.getProvinceModal = self.getBackProvinceModal;
        ReHosDesk.getCityModal = self.getbackCityModal;
    }
    if (indexPath.row == 2) {
        ReHosDesk.loadRequestNum = @"2";
        //
        ReHosDesk.getProvinceModal = self.getBackProvinceModal;
        ReHosDesk.getCityModal = self.getbackCityModal;
        ReHosDesk.getHospitalModal = self.getBackHospitalModal;
    }
    
    ReHosDesk.navBarTitle = self.selecteTypes[indexPath.row];
    [self.navigationController pushViewController:ReHosDesk animated:YES];
    
}

-(void)getAreaProvinceModal:(AreaModal *)provinceModal andCityModal:(AreaModal *)cityModal andHospitalModal:(RegistrationHospitalModal *)hospitalModal andParentDepartmentModal:(RegistrationDepartmentModal *)parDepartmentModal andChildDepartment:(RegistrationDepartmentModal *)childDepartment
{
    [self.finishedDic setValue:cityModal.provinceid forKey:@"provinceid"];
    [self.finishedDic setValue:cityModal.cityid forKey:@"cityid"];
    [self.finishedDic setValue:childDepartment.hospitalid forKey:@"hospitalid"];
    //一级科室parhospdeptid
    [self.finishedDic setValue:parDepartmentModal.hospdeptid forKey:@"parhospdeptid"];
    //二级科室childhospdeptid
    [self.finishedDic setValue:childDepartment.hospdeptid forKey:@"hospdeptid"];
    [self.finishedDic setValue:childDepartment.level forKey:@"hospitallevel"];
    
    
    
    
    MyNSLog("%@%@", parDepartmentModal.level,parDepartmentModal.levelname);
    
    //医院id是一样的parDepartmentModal.hospitalid和parDepartmentModal.hospitalid。
    //parDepartmentModal.hospdeptid这是一级科室，childDepartment.hospitalid是下一级科室
    MyNSLog("%@,%@---%@,%@ %@", parDepartmentModal.hospdeptid,parDepartmentModal.hospitalid,childDepartment.hospdeptid,childDepartment.hospitalid,childDepartment.level);

    //选择的是新值
    if (self.selectedCell.detailLabel.text.length == 0)
    {
        if (self.selectedIndexPath.row == 0)//点击的是第0行“地区”
        {
            [self.getDataArray replaceObjectAtIndex:0 withObject:[NSString stringWithFormat:@"%@ %@",provinceModal.name,cityModal.name]];
            [self.getDataArray addObject:@""];
            [self.tableView reloadData];
        }else if (self.selectedIndexPath.row == 1)//点击的是“医院”
        {
            [self.getDataArray replaceObjectAtIndex:1 withObject:hospitalModal.hospitalname];
            [self.getDataArray addObject:@""];
            [self.tableView reloadData];
        }
        else//点击的是“科室”
        {
            if (childDepartment == nil) {
                [self.getDataArray replaceObjectAtIndex:2 withObject:parDepartmentModal.departmentname];
            }else
            {
                [self.getDataArray replaceObjectAtIndex:2 withObject:childDepartment.departmentname];
            }
            [self.tableView reloadData];
            
        }
        //必须在最后把这个值付给
        self.getBackProvinceModal = provinceModal;
        self.getbackCityModal = cityModal;
        self.getBackHospitalModal = hospitalModal;
        

 
    }
    else//要重新选
    {
        NSString * cellText = self.selectedCell.detailLabel.text;
        //重新选地区
        if (self.selectedIndexPath.row == 0)
        {
            if ([cellText isEqualToString:[NSString stringWithFormat:@"%@ %@",provinceModal.name,cityModal.name]]) {
                return;
            }else
            {
                [self.getDataArray removeAllObjects];
                [self.getDataArray addObject:[NSString stringWithFormat:@"%@ %@",provinceModal.name,cityModal.name]];
                [self.getDataArray addObject:@""];
                [self.tableView reloadData];
                
                //必须在最后把这个值付给
                self.getBackProvinceModal = provinceModal;
                self.getbackCityModal = cityModal;
                //此时这个医院是nil
                self.getBackHospitalModal = hospitalModal;
            }
 
        }else if (self.selectedIndexPath.row == 1)//重新选医院
        {
            if ([cellText isEqualToString:hospitalModal.hospitalname]) {
                return;
            }else
            {
                [self.getDataArray replaceObjectAtIndex:2 withObject:@""];
                [self.tableView reloadData];
                //必须在最后把这个值付给
                self.getBackProvinceModal = provinceModal;
                self.getbackCityModal = cityModal;
                self.getBackHospitalModal = hospitalModal;

            }
        }else
        {
            if (childDepartment == nil) {
                [self.getDataArray replaceObjectAtIndex:2 withObject:parDepartmentModal.departmentname];
            }else
            {
                [self.getDataArray replaceObjectAtIndex:2 withObject:childDepartment.departmentname];
            }
            [self.tableView reloadData];
            //必须在最后把这个值付给
            self.getBackProvinceModal = provinceModal;
            self.getbackCityModal = cityModal;
            self.getBackHospitalModal = hospitalModal;
        }

    }
  

}

-(void)viewWillAppear:(BOOL)animated
{
    if (self.getDataArray.count == 3 && [self.getDataArray[2] length] != 0) {
        [self makeRegistrationButton];
    }
}

-(void)makeRegistrationButton
{
    self.registrationButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.registrationButton setFrame:CGRectMake(15, kScreenHeight-47-30-iOS7TopMargin, kScreenWidth-30, 47)];
    self.registrationButton.backgroundColor = [UIColor colorWithRed:35/255.0 green:205/255.0 blue:235/255.0 alpha:1];
    [self.registrationButton setTitle:@"确认挂号" forState:UIControlStateNormal];
    [self.registrationButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.registrationButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.registrationButton addTarget:self action:@selector(registrationButtonDown) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.registrationButton];


}
-(void)registrationButtonDown{

    RegDoctorListViewController * docList = [[RegDoctorListViewController alloc] init];
    
    docList.regHosDepartDic = self.finishedDic;
    [self.navigationController pushViewController:docList animated:YES];


}

@end
